package vip.zhenzicheng.algorithm.sort;

import static vip.zhenzicheng.algorithm.util.ArrayUtils.swap;

/**
 * 选择排序
 *
 * @author zhenzicheng
 * @date: 2022-05-16 19:53
 */
public class SelectionSort {
  public static void main(String[] args) {
    int[] arr = {1, 5, 7, 2, 3, 4, 7, 8, 6, 11, 6, 65, 361, 11, 2, 7, 3};

    selectSort(arr);
    printArray(arr);
  }

  public static void selectSort(int[] arr) {
    if (arr == null || arr.length < 2) {
      return;
    }
    int N = arr.length;
    for (int i = 0; i < N; i++) {
      int minValueIndex = i;
      for (int j = i + 1; j < N; j++) {
        minValueIndex = arr[j] < arr[minValueIndex] ? j : minValueIndex;
      }
      swap(arr, i, minValueIndex);
    }
  }

  public static void printArray(int[] arr) {
    for (int i = 0; i < arr.length; i++) {
      System.out.print(arr[i] + " ");
    }
    System.out.println();
  }

}
